package com.cskaoyan.mall.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cskaoyan.mall.common.constant.CodeEnum;
import com.cskaoyan.mall.common.constant.ResultCodeEnum;
import com.cskaoyan.mall.common.execption.BusinessException;
import com.cskaoyan.mall.user.consts.UserConstants;
import com.cskaoyan.mall.user.dto.UserLoginDTO;
import com.cskaoyan.mall.user.dto.UserLoginInfoDTO;
import com.cskaoyan.mall.user.mapper.UserInfoMapper;
import com.cskaoyan.mall.user.model.UserInfo;
import com.cskaoyan.mall.user.query.UserInfoParam;
import com.cskaoyan.mall.user.service.UserService;
import org.apache.commons.codec.binary.Hex;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.concurrent.TimeUnit;


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    RedissonClient redissonClient;
    @Autowired
    UserInfoMapper userInfoMapper;
    @Override
    public UserLoginDTO login(UserInfoParam userInfoParam, String ip, String token) {
        //token是通过controller层获取的
        //要先验证用户名和密码是否是合规的
        //这个地方需要对密码进行一个加密
        String encodePasswd=encode(userInfoParam.getPasswd());
        System.out.println(encodePasswd+"密码密码");
//        String encodePasswd= DigestUtils.md5DigestAsHex(userInfoParam.getPasswd().getBytes());

        LambdaQueryWrapper<UserInfo> userInfoLambdaQueryWrapper=new LambdaQueryWrapper<>();
        userInfoLambdaQueryWrapper.eq(UserInfo::getLoginName, userInfoParam.getLoginName());
        userInfoLambdaQueryWrapper.eq(UserInfo::getPasswd, encodePasswd);
        UserInfo userInfo = userInfoMapper.selectOne(userInfoLambdaQueryWrapper);
        if (userInfo!=null){
            //然后呢，创建redis的id
            String prefixName= UserConstants.USER_LOGIN_KEY_PREFIX+token;
            //这里要定义超时时间
            RBucket<UserLoginInfoDTO> bucket = redissonClient.getBucket(prefixName);
            UserLoginInfoDTO userLoginInfoDTO=new UserLoginInfoDTO();
            userLoginInfoDTO.setUserId(String.valueOf(userInfo.getId()));
            userLoginInfoDTO.setIp(ip);
            bucket.set(userLoginInfoDTO,UserConstants.USERKEY_TIMEOUT, TimeUnit.SECONDS);
            UserLoginDTO userLoginDTO=new UserLoginDTO(userInfo.getNickName(),token);
            return userLoginDTO;
        }else {
            //如果没有查找到对应用户的话，抛出异常
            throw new BusinessException(ResultCodeEnum.FAIL);
        }



    }

    @Override
    public void logout(String token) {
        RBucket<Object> bucket = redissonClient.getBucket(UserConstants.USER_LOGIN_KEY_PREFIX + token);
        if (bucket!=null){
            bucket.delete();
        }

    }

    //加密
    public String encode(String str){
        try {
            MessageDigest md=MessageDigest.getInstance("SHA-1");
            md.update(str.getBytes(StandardCharsets.UTF_8));
            byte[] digest=md.digest();
            return String.valueOf(Hex.encodeHex(digest));
        } catch (Exception e) {
            e.printStackTrace();
            return str;
        }
    }


    public static void main(String[] args) {
        UserServiceImpl userService = new UserServiceImpl();
        System.out.println(userService.encode("1111"));
    }
}
